package com.pla.googlecalendarviews;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.AbstractTableModel;
import org.wonderly.awt.Packer;

public class AccountsDialog extends JDialog implements ListSelectionListener {

  private final JButton addButton = new JButton("Add");
  private final JButton modifyButton = new JButton("Modify");
  private final JButton deleteButton = new JButton("Delete");
  private final JButton cancelButton = new JButton("Close");
  private ArrayList<Account> accounts = new ArrayList<Account>();
  private AccountsTableModel model;
  private JLabel message = new JLabel(" ");
  private JTable table;

  public AccountsDialog(final JFrame parent) {
    super(parent, "Google Accounts", true);
    Utils.makeDisposeOnEscapeKey(this);
    JPanel panel = new JPanel();
    Packer packer = new Packer(panel);
    setContentPane(panel);
    int y = 0;
    model = new AccountsTableModel();
    table = new JTable(model);
    table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    table.getSelectionModel().addListSelectionListener(this);
    JScrollPane scrollPane = new JScrollPane(table);
    packer.pack(scrollPane).gridx(0).gridy(y++).fillboth();
    JPanel buttonHolder = new JPanel();
    buttonHolder.add(addButton);
    buttonHolder.add(modifyButton);
    buttonHolder.add(deleteButton);
    buttonHolder.add(cancelButton);
    packer.pack(buttonHolder).gridx(0).gridy(y++);
    packer.pack(message).gridx(0).gridy(y++);
    message.setForeground(Color.red);
    cancelButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        dispose();
      }
    });
    addButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        Account account = new Account();
        new AccountDialog(parent, account);
        if (account.isExists()) {
          accounts.add(account);
          model.fireTableDataChanged();
        }
      }
    });
    modifyButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        int index = table.getSelectionModel().getMinSelectionIndex();
        if (index >= 0) {
          Account account = accounts.get(index);
          new AccountDialog(parent, account);
          model.fireTableDataChanged();
        }
      }
    });
    deleteButton.addActionListener(new ActionListener() {

      public void actionPerformed(ActionEvent e) {
        int index = table.getSelectionModel().getMinSelectionIndex();
        if (index >= 0) {
          Account account = accounts.get(index);
          if (JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(parent, "Are you sure you want to delete " + account.getEmailAddress() + "?")) {
            AccountDAO accountDAO = new AccountDAO();
            accountDAO.delete(account);
            accounts.remove(index);
            model.fireTableDataChanged();
          }
        }
      }
    });
    addButton.setMnemonic(KeyEvent.VK_A);
    modifyButton.setMnemonic(KeyEvent.VK_M);
    deleteButton.setMnemonic(KeyEvent.VK_D);
    cancelButton.setMnemonic(KeyEvent.VK_C);
    modifyButton.setEnabled(false);
    deleteButton.setEnabled(false);
    pack();
    setLocationRelativeTo(parent);
    loadAccounts();
    setVisible(true);
  }

  public void valueChanged(ListSelectionEvent e) {
    if (e.getValueIsAdjusting()) {
      return;
    }
    modifyButton.setEnabled(false);
    deleteButton.setEnabled(false);
    int index = e.getFirstIndex();
    if (index >= 0) {
      modifyButton.setEnabled(true);
      deleteButton.setEnabled(true);
    }
  }

  class AccountsTableModel extends AbstractTableModel {

    private String[] columns = {"Email address"};

    public String getColumnName(int column) {
      return columns[column];
    }

    public AccountsTableModel() {
    }

    public int getRowCount() {
      return accounts.size();
    }

    public int getColumnCount() {
      return columns.length;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
      Account googleAccount = (Account) accounts.get(rowIndex);
      switch (columnIndex) {
        case 0:
          return googleAccount.getEmailAddress();
      }
      return null;
    }
  }

  private void loadAccounts() {
    SwingWorker worker = new SwingWorker() {

      protected Object doInBackground() throws Exception {
        AccountDAO dao = new AccountDAO();
        accounts.addAll(dao.get());
        return null;
      }

      protected void done() {
        model.fireTableDataChanged();
      }
    };
    worker.execute();
  }
}
